package com.gxa.uhome.exception;

import com.gxa.uhome.util.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class MyExceptionHandler {


    /**
     * 捕获系统异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(SystemException.class)
    @ResponseBody
    public JsonResult systemExceptionHandler(SystemException e) {
        // 记录日志
        final JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(e.getMsg());
        jsonResult.setCode(e.getCode()+"");
        return jsonResult;
    }

    /**
     * 捕获系统异常-非ajax请求
     *
     * @param e
     * @return
     */
    @ExceptionHandler(com.gxa.uhome.exception.NoAjaxException.class)
    public ModelAndView noAjaxExceptionHandler(com.gxa.uhome.exception.NoAjaxException e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error");
        return modelAndView;
    }


    /**
     * 捕获所有异常
     *
     * @param e
     * @return
     */
    //@ExceptionHandler(Exception.class)
    public JsonResult exceptionHandler(Exception e) {
        // 记录日志
        final JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1001");
        jsonResult.setMsg("未知异常");
        return jsonResult;
    }


}
